package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.system.domain.ApplyTable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ServerType;
import com.ruoyi.system.service.IServerTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 服务分类Controller
 * 
 * @author ruoyi
 * @date 2021-11-13
 */
@Controller
@RequestMapping("/server/type")
public class ServerTypeController extends BaseController
{
    private String prefix = "server/type";

    @Autowired
    private IServerTypeService serverTypeService;

    @RequiresPermissions("server:type:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询服务分类树列表
     */
    @RequiresPermissions("server:type:list")
    @PostMapping("/list")
    @ResponseBody
    public List<ServerType> list(ServerType serverType)
    {
        List<ServerType> list = serverTypeService.selectServerTypeList(serverType);
        return list;
    }

    /**
     * 导出服务分类列表
     */
    @RequiresPermissions("server:type:export")
    @Log(title = "服务分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ServerType serverType)
    {
        List<ServerType> list = serverTypeService.selectServerTypeList(serverType);
        ExcelUtil<ServerType> util = new ExcelUtil<ServerType>(ServerType.class);
        return util.exportExcel(list, "服务分类数据");
    }

    /**
     * 新增服务分类
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("serverType", serverTypeService.selectServerTypeById(id));
        }else{
            ServerType serverType = new ServerType();
            serverType.setId(0L);
            mmap.put("serverType", serverType);
        }
        return prefix + "/add";
    }

    /**
     * 新增保存服务分类
     */
    @RequiresPermissions("server:type:add")
    @Log(title = "服务分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ServerType serverType)
    {
        return toAjax(serverTypeService.insertServerType(serverType));
    }

    /**
     * 修改服务分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ServerType serverType = serverTypeService.selectServerTypeById(id);
        mmap.put("serverType", serverType);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务分类
     */
    @RequiresPermissions("server:type:edit")
    @Log(title = "服务分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ServerType serverType)
    {
        return toAjax(serverTypeService.updateServerType(serverType));
    }

    /**
     * 删除
     */
    @RequiresPermissions("server:type:remove")
    @Log(title = "服务分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        ServerType serverType = new ServerType();
        serverType.setParentid(id);
        if( serverTypeService.selectServerTypeList(serverType).size() != 0) {
            return error("存在子节点不允许删除");
        }
        return toAjax(serverTypeService.deleteServerTypeById(id));
    }

    /**
     * 选择服务分类树
     */
    @GetMapping(value = { "/selectTypeTree/{id}", "/selectTypeTree/" })
    public String selectTypeTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("serverType", serverTypeService.selectServerTypeById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载服务分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = serverTypeService.selectServerTypeTree();
        return ztrees;
    }
}
